﻿using PBO.Data;
using PBO.Data.Rnd;
using PBO.Game;
using PBO.Network;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

namespace PBO.Game.BattleClass
{
    public partial class Battle
    {

        private void EnTrapMove(int moveId, Pokemon user, Pokemon target)
        {
            int[] arr = { 20, 35, 128 };

            if (arr.Contains(moveId))
            {
                log.AppendMsg("EnTrap" + moveId, target.GetNameString(), user.GetNameString());
            }
            else
            {
                log.AppendMsg("EnTrap" + moveId, target.GetNameString());
            }
        }

        private void AssistTarget(Move selMove, Pokemon user, Pokemon target, bool moveCall)
        {
            if (!CanAttackTarget(selMove, target, user))
                return;
            var moveType = selMove.Type;
            if (user.GetSelTrait() == Trait.Normalize)
                moveType = BattleType.Normal;
            //电磁波
            if (selMove.Data.Id == 86 && CalculateAttackTypeEffect(user, target, moveType) == 0)
            {
                log.AppendMsg("NoEffect", target.GetNameString());
                //log.AddText("这对" + target.GetNameString() + "没有效果...", Color.Red);
                return;
            }
            if (target.GetSelTrait() == Trait.Soundproof && selMove.Sound)
            {
                target.ShowTrait();
                log.AppendMsg("NoEffect", target.GetNameString());
                //log.AppendText(target.GetNameString() + "的隔音特性使得技能无效！");
                return;
            }
            if (target.BattleState.substituted && selMove.Substitute)
            {
                log.AppendMsg("Fail", target.GetNameString());
                //log.AppendText(target.GetNameString() + "的替身使" + selMove.Data.Name + "无效了！");
                return;
            }
            double acc = CalculateAcc(user, target, selMove);
            if (_random.NextDouble() > acc)
            {
                log.AppendMsg("Miss", target.GetNameString());
                //log.AddText(user.CustomName + "的技能没有命中...");
                return;
            }
            if (target.BattleState.magicCoat && selMove.Data.Effect <= MoveEffect.种子)
            {
                log.AppendMsg("MagicCoat", target.GetNameString(), selMove.Data.Name);
                //log.AddText(user.CustomName + "的技能效果被反弹回来！");
                target = user;
            }
            if (selMove.Snatchable && user.BattleState.snatched && user.BattleState.snatchBy.HealthPower != 0)
            {
                log.AppendMsg("Snatch", user.BattleState.snatchBy.GetNameString(), user.GetNameString());
                //log.AddText(user.GetNameString() + "的技能效果被" + user.BattleState.snatchBy.GetNameString() + "抢夺了！");
                if (selMove.Data.Effect == MoveEffect.自我暗示)
                {
                    target = user;
                    user = user.BattleState.snatchBy;
                }
                else if (selMove.Data.Effect == MoveEffect.点穴)
                {
                    target = user.BattleState.snatchBy;
                }
            }
            switch (selMove.Data.Effect)
            {
                case MoveEffect.中毒:
                    if (!target.SetState(PokemonState.Poison, user, true, false, false, false, true))
                        Fail();
                    break;
                case MoveEffect.麻痹:
                    if (!target.SetState(PokemonState.Paralysis, user, true, false, false, false, true))
                        Fail();
                    break;
                case MoveEffect.烧伤:
                    if (!target.SetState(PokemonState.Burn, user, true, false, false, false, true))
                        Fail();
                    break;
                case MoveEffect.冻结:
                    if (!target.SetState(PokemonState.Freeze, user, true, false, false, false, true))
                        Fail();
                    break;
                case MoveEffect.睡眠:
                    if (!target.SetState(PokemonState.Sleep, user, true, false, false, false, true))
                        Fail();
                    break;
                case MoveEffect.剧毒:
                    if (!target.SetState(PokemonState.Toxin, user, true, false, false, false, true))
                        Fail();
                    break;
                case MoveEffect.混乱:
                    if (!target.confuse(moveCall, user))
                        Fail();
                    break;
                case MoveEffect.种子:
                    target.BeSeeded(GetPokemonIndex(user));

                    break;
                case MoveEffect.着迷:
                    if (!target.Captivate(user))
                        Fail();
                    break;
                case MoveEffect.束缚:
                    target.BattleState.constraint = true;
                    target.BattleState.constraintTurn = -1;
                    target.BattleState.constraintBy = GetPokemonIndex(user);
                    EnTrapMove(selMove.Data.Id, user, target);
                    //log.AddText(target.GetNameString() + "不能逃走了！");
                    break;
                case MoveEffect.诅咒:
                    if (target.BattleState.curse == false)
                    {
                        target.BattleState.curse = true;
                        log.AppendMsg("Curse", target.GetNameString());
                        //log.AddText(target.GetNameString() + "被诅咒了！");
                        user.HurtFraction(2);
                    }
                    else
                    {
                        Fail();
                    }
                    break;
                case MoveEffect.噩梦:
                    if (target.State == PokemonState.Sleep)
                    {
                        if (target.BattleState.nightmare == false)
                        {
                            target.BattleState.nightmare = true;
                            log.AppendMsg("EnNightmare", target.GetNameString());
                            //log.AddText(target.GetNameString() + "被噩梦困扰！");
                        }
                        else
                        {
                            log.AppendMsg("Nightmare", target.GetNameString());
                            //log.AddText(target.GetNameString() + "已经处于噩梦状态");
                        }
                    }
                    else
                    {
                        Fail();
                    }
                    break;
                case MoveEffect.鼓掌:
                    if (!target.Encore())
                    {
                        Fail();
                    }

                    break;
                case MoveEffect.挑拨:
                    target.Taunt();

                    break;
                case MoveEffect.锁定:
                    target.BattleState.lockOn = true;
                    target.BattleState.lockOnCounter = 2;
                    log.AppendMsg("LockOn", user.GetNameString(), target.GetNameString());
                    //log.AppendText(target.GetNameString() + "无法回避！");
                    break;
                case MoveEffect.精神控制:
                    if (!target.LastMoveIsValid || target.BattleState.DisableIndex != 0)
                    {
                        Fail();
                    }
                    else
                    {
                        target.BattleState.DisableIndex = target.lastMoveIndex;
                        target.BattleState.DisableTurn = _random.Next(2, 6);
                        target.BattleState.DisableCounter = 0;
                        log.AppendMsg("FailSp", target.GetNameString(), target.GetSelMove(target.BattleState.DisableIndex).Data.Name);
                        //log.AppendText(target.GetNameString() + "的" + target.GetSelMove(target.BattleState.DisableIndex).Name + "技能不能使用了！");
                    }
                    break;
                case MoveEffect.强制交换:
                    if (!target.MakedToSwap(user))
                        Fail();
                    break;
                case MoveEffect.哈欠:
                    if (!target.BeYawned())
                        Fail();
                    break;
                case MoveEffect.临别礼物:
                    target.AbilityLvDownChanged(StatType.Atk, user, 2, true);
                    target.AbilityLvDownChanged(StatType.SpAtk, user, 2, true);
                    break;
                case MoveEffect.念力挪移:
                    if (user.State != PokemonState.No && target.SetState(user.State, user, false))
                    {
                        user.State = PokemonState.No;
                        log.AppendMsg("StateReset", user.GetNameString());
                        //log.AddText(user.GetNameString() + "恢复了状态！");
                    }
                    else
                    {
                        Fail();
                    }
                    break;
                case MoveEffect.恨:
                    if (target.LastMoveIsValid)
                    {
                        Move targetMove = target.LastMove;
                        int move_pp = Math.Min(targetMove.PP, 4);
                        log.AppendMsg("Spite", target.GetNameString(), targetMove.Data.Name, move_pp);
                        //log.AddText(target.GetNameString() + "的" + targetMove.Name + "技能PP减少了");
                        targetMove.PP -= move_pp;
                        //targetMove.SetPP(targetMove.PP - move_pp);
                    }
                    break;
                case MoveEffect.分担痛苦:
                    int hp = (user.HealthPower + target.HealthPower) / 2;
                    user.SetHP(hp);
                    target.SetHP(hp);
                    log.AppendMsg("PainSplit", user.GetNameString(), target.GetNameString());
                    //log.AddText("双方的HP平分了");
                    break;
                case MoveEffect.寻衅:
                    if (target.BattleState.torment)
                    {
                        Fail();
                    }
                    else
                    {
                        target.BattleState.torment = true;
                        log.AppendMsg("subtitle_Torment", target.GetNameString());
                        //log.AddText(target.GetNameString() + "的技能不能连续使用！");
                    }
                    break;
                case MoveEffect.煽动:
                    user.AbilityLvUpChanged(StatType.SpAtk, 1, false);
                    //target.SAtkLVUp(1);
                    target.confuse(moveCall, user);
                    break;
                case MoveEffect.虚张声势:
                    user.AbilityLvUpChanged(StatType.Atk, 2, false);
                    //target.AtkLVUp(2);
                    target.confuse(moveCall, user);
                    break;
                case MoveEffect.识破:
                    target.BattleState.foresight = true;
                    log.AppendMsg("CanAttack", target.GetNameString());
                    //log.AppendText(target.GetNameString() + "被识破了！");
                    break;
                case MoveEffect.回复封印:
                    if (target.BattleState.healBlock)
                    {
                        Fail();
                    }
                    else
                    {
                        target.BattleState.healBlock = true;
                        target.BattleState.healBlockCounter = 5;
                        log.AppendMsg("HealBlock", target.GetNameString());
                        //log.AddText(target.GetNameString() + "禁止回复！");
                    }
                    break;
                case MoveEffect.力量交换:
                    int temp = 0;
                    temp = user.AttackLV;
                    user.BattleState.AbilityLv[StatType.Atk] = target.AttackLV;
                    target.BattleState.AbilityLv[StatType.Atk] = temp;
                    temp = user.SpAttackLV;
                    user.BattleState.AbilityLv[StatType.SpAtk] = target.SpAttackLV;
                    target.BattleState.AbilityLv[StatType.SpAtk] = temp;
                    log.AppendMsg("PowerSwap", user.CustomName, target.GetNameString());
                    //log.AddText(user.CustomName + "和" + target.GetNameString() + "交换了攻击等级！");
                    break;
                case MoveEffect.防御交换:
                    int fyjh = 0;
                    fyjh = user.DefenceLV;
                    user.BattleState.AbilityLv[StatType.Def] = target.DefenceLV;
                    target.BattleState.AbilityLv[StatType.Def] = fyjh;
                    fyjh = user.SpDefenceLV;
                    user.BattleState.AbilityLv[StatType.SpDef] = target.SpDefenceLV;
                    target.BattleState.AbilityLv[StatType.SpDef] = fyjh;
                    log.AppendMsg("GuardSwap", user.CustomName, target.GetNameString());
                    //log.AddText(user.CustomName + "和" + target.GetNameString() + "交换了防御等级！");
                    break;
                case MoveEffect.心灵交换:
                    BattleStats temp11 = (BattleStats)user.BattleState.AbilityLv.Clone();
                    user.BattleState.AbilityLv = (BattleStats)target.BattleState.AbilityLv.Clone();
                    target.BattleState.AbilityLv = temp11;
                    log.AppendMsg("HeartSwap", user.CustomName, target.GetNameString());
                    //log.AddText(user.CustomName + "和" + target.GetNameString() + "交换了能力等级！");
                    break;
                case MoveEffect.换装:
                    if (target.GetSelTrait(true) == Trait.WonderGuard || target.GetSelTrait(true) == Trait.Forecast)
                    {
                        Fail();
                    }
                    else
                    {
                        log.AppendMsg("SetAbility", user.CustomName, target.GetSelTrait(true));
                        //log.AddText(user.CustomName + "的特性变成了" + target.GetSelTrait(true).ToString() + "！");
                        user.SetTrait(target.GetSelTrait(true));
                    }
                    break;
                case MoveEffect.戏法:
                    if ((user.GetItem(true) == Item.None && target.GetItem(true) == Item.None) || (target.GetSelTrait() == Trait.StickyHold && user.GetSelTrait() != Trait.MoldBreaker) || (user.GetSelTrait() == Trait.StickyHold && target.GetSelTrait() != Trait.MoldBreaker))
                    {
                        Fail();
                    }
                    else
                    {
                        log.AppendMsg("Trick", user.CustomName, target.GetNameString());
                        //log.AddText(user.CustomName + "和" + target.GetNameString() + "交换了道具！");
                        Item temp2 = user.GetItem(true);
                        user.SetItem(target.GetItem(true));
                        target.SetItem(temp2);
                    }
                    break;
                case MoveEffect.扣押:
                    if (target.BattleState.detain)
                    {
                        Fail();
                    }
                    else
                    {
                        target.BattleState.detain = true;
                        target.BattleState.detainCounter = 5;
                        log.AppendMsg("EnEmbargo", target.GetNameString());
                        //log.AddText(target.GetNameString() + "的道具无效了！");
                    }
                    break;
                case MoveEffect.抢夺:
                    target.BattleState.snatched = true;
                    target.BattleState.snatchBy = user;
                    break;
                case MoveEffect.胃液:
                    if (target.BattleState.gastricJuice == false)
                    {
                        target.BattleState.worrySeed = false;
                        target.BattleState.gastricJuice = true;
                        log.AppendMsg("EnGastroAcid", target.GetNameString());
                        //log.AddText(target.GetNameString() + "的特性无效了");
                    }
                    else
                    {
                        Fail();
                    }

                    break;
                case MoveEffect.素描:
                    if (target.LastMoveIsValid && !user.HaveMove(target.GetSelMoveId(target.lastMoveIndex)))
                    {
                        //Move move = new Move(GameService.GetMove(target.LastMove.MoveId), false);
                        //user.SetMove(target.lastMoveIndex, move);
                        user.SetMove(target.lastMoveIndex, target.LastMove.Data.Id, 0);
                        user.lastMoveIndex = 0;
                    }
                    else
                    {
                        Fail();
                    }
                    break;
                case MoveEffect.模仿:
                    if (target.LastMoveIsValid && !user.HaveMove(target.GetSelMoveId(target.lastMoveIndex)))
                    {
                        var move = user.SetMove(target.lastMoveIndex, target.LastMove.Data.Id, 0);
                        move.PP = 5;
                        user.lastMoveIndex = 0;
                    }
                    else
                    {
                        Fail();
                    }

                    break;
                case MoveEffect.变身:
                    if (!target.BattleState.transfrom)
                    {
                        log.AppendMsg("Transform", user.CustomName, target.NameBase);
                        //log.AddText(user.CustomName + "变成了" + target.NameBase + "！");
                        user.Transfrom(target);
                        user.lastMoveIndex = 0;
                    }
                    else
                    {
                        Fail();
                    }
                    break;
                case MoveEffect.回避减一:
                    target.AbilityLvDownChanged(StatType.Evasion, user, 1, true);
                    break;
                case MoveEffect.命中减一:
                    target.AbilityLvDownChanged(StatType.Accuracy, user, 1, true);
                    break;
                case MoveEffect.速度减一:
                    target.AbilityLvDownChanged(StatType.Speed, user, 1, true);
                    break;
                case MoveEffect.速度减二:
                    target.AbilityLvDownChanged(StatType.Speed, user, 2, true);
                    break;
                case MoveEffect.物攻减一:
                    target.AbilityLvDownChanged(StatType.Atk, user, 1, true);
                    break;
                case MoveEffect.物攻减二:
                    target.AbilityLvDownChanged(StatType.Atk, user, 2, true);
                    break;
                case MoveEffect.物防减一:
                    target.AbilityLvDownChanged(StatType.Def, user, 1, true);
                    break;
                case MoveEffect.物防减二:
                    target.AbilityLvDownChanged(StatType.Def, user, 2, true);
                    break;
                case MoveEffect.特攻减一:
                    target.AbilityLvDownChanged(StatType.SpAtk, user, 1, true);
                    break;
                case MoveEffect.特攻减二:
                    target.AbilityLvDownChanged(StatType.SpAtk, user, 2, true);
                    break;
                case MoveEffect.诱惑:
                    if (target.Gender != PokemonGender.None && user.Gender != PokemonGender.None && target.Gender != user.Gender)
                    {
                        target.AbilityLvDownChanged(StatType.SpAtk, user, 2, true);
                    }
                    else
                    {
                        Fail();
                    }
                    break;
                case MoveEffect.特防减一:
                    target.AbilityLvDownChanged(StatType.SpDef, user, 1, true);
                    break;
                case MoveEffect.特防减二:
                    target.AbilityLvDownChanged(StatType.SpDef, user, 2, true);
                    break;
                case MoveEffect.物理攻防减一:
                    target.AbilityLvDownChanged(StatType.Atk, user, 1, true);
                    target.AbilityLvDownChanged(StatType.Def, user, 1, true);
                    break;
                case MoveEffect.自我暗示:
                    user.BattleState.AbilityLv = (BattleStats)target.BattleState.AbilityLv.Clone();
                    log.AppendMsg("PsychUpPsychUp", user.CustomName, target.GetNameString());
                    //log.AddText(user.CustomName + "复制了" + target.GetNameString() + "的能力等级！");
                    break;
                case MoveEffect.奇迹之眼:
                    target.BattleState.miracleEye = true;
                    break;
                case MoveEffect.点穴:
                    var types = (from t in BattleStats.Type7D
                                 where target.BattleState.AbilityLv[t] < 6
                                 select t).ToArray();
                    if (types == null || types.Length == 0)
                    {
                        var type = types[_random.Next(types.Length)];
                        target.AbilityLvUpChanged(type, 2, type != StatType.Evasion);
                    }
                    break;
                case MoveEffect.除雾:
                    target.AbilityLvDownChanged(StatType.Evasion, user, 1, true);
                    target.MyTeam.Group.BreakMist();
                    break;
                case MoveEffect.先取:
                    if (!target.BattleState.moved)
                    {
                        Move move = default(Move);
                        foreach (PlayerBattleMove item in atkList)
                        {
                            if (object.ReferenceEquals(item.PM, target))
                            {
                                move = GetPokemonMove(item);
                                if (move.Data.Category == MoveCategory.变化)
                                    return;
                                log.AppendMsg("Snatch", user.CustomName, target.GetNameString());
                                //log.AddText(target.GetNameString() + "的技能被先取了！");
                                user.BattleState.meFirst = true;
                                this.MoveCall(move, user);
                                user.BattleState.meFirst = false;
                                break; // TODO: might not be correct. Was : Exit For
                            }
                        }
                    }
                    else
                    {
                        Fail();
                    }
                    break;
                case MoveEffect.眩晕之舞:
                    target.confuse(false, user);

                    break;
                case MoveEffect.属性切换2:
                    if (target.LastMoveIsValid)
                    {
                        user.Type1 = target.LastMove.Type.Conversion2Type(_random);
                        log.AppendMsg("TypeChange", user.CustomName, user.Type1);
                        //log.AddText(user.CustomName + "的属性变成了" + user.Type1.ToCnName());
                    }
                    else
                    {
                        Fail();
                    }

                    break;
                case MoveEffect.鹦鹉学舌:
                    if (target.LastMoveIsValid)
                    {
                        Move move = new Move(target.LastMove.Data.Id, null, 0);
                        this.MoveCall(move, user);
                    }
                    else
                    {
                        Fail();
                    }
                    break;
                case MoveEffect.特性交换:
                    if (target.GetSelTrait(true) == Trait.WonderGuard || target.GetSelTrait(true) == Trait.Forecast)
                    {
                        Fail();
                    }
                    else
                    {
                        log.AppendMsg("SkillSwap", user.CustomName, target.GetNameString());
                        //log.AddText(user.CustomName + "和" + target.GetNameString() + "交换了特性！");
                        Trait txjh = user.GetSelTrait(true);
                        user.SetTrait(target.GetSelTrait(true));
                        target.SetTrait(txjh);
                    }
                    break;
                //case MoveEffect.单纯光线:
                //    if (target.GetSelTrait() == Trait.偷懒)
                //    {
                //        Fail();
                //    }
                //    else
                //    {
                //        target.SetTrait(Trait.单纯);
                //        log.AddText(target.GetNameString() + "的特性变成了单纯！");
                //    }
                //    break;
                case MoveEffect.烦恼之种:
                    if (target.BattleState.worrySeed || target.GetSelTrait() == Trait.Truant)
                    {
                        Fail();
                    }
                    else
                    {
                        target.BattleState.worrySeed = true;
                        log.AppendMsg("SetAbility", target.GetNameString(), Trait.Insomnia);
                        //log.AddText(target.GetNameString() + "的特性变成了不眠！");
                        target.RaiseTrait();
                    }
                    break;
                //case MoveEffect.变为同伴:
                //    target.SetTrait(user.SelTrait(true));
                //    log.AddText(string.Format("{0}的特性变成了{1}！", target.GetNameString, user.SelTrait(true).ToString()));

                //    break;
                case MoveEffect.HP一半恢复:
                    target.RecoverPokemonHP(target.MaxHp >> 1);
                    break;
                //case MoveEffect.镜面属性:
                //    user.Type1 = target.Type1;
                //    user.Type2 = target.Type2;

                //    log.AddText(string.Format("{0}复制了{1}的属性", user.CustomName, target.CustomName));
                //    break;
                //case MoveEffect.浸水:
                //    PBO.Data.Type type = BattleData.GetTypeData("水");
                //    target.Type1 = type;
                //    target.Type2 = null;
                //    log.AddText(target.CustomName + "的属性变成了" + type.Name + "！");
                //    break;
                //case MoveEffect.传递礼物:
                //    if (target.GetItem(true) == Item.无 && user.GetItem(true) != Item.无)
                //    {
                //        target.SetItem(user.GetItem(true));
                //        user.SetItem(Item.无);
                //        log.AddText(string.Format("{0}将道具送给了{1}", user.CustomName, target.CustomName));
                //    }
                //    break;
                //case MoveEffect.您先请:
                //    if (!BringToAttackListFront(target))
                //    {
                //        Fail();
                //    }
                //    break;
                //case MoveEffect.押后:
                //    if (BringToAttackListBack(target))
                //    {
                //        log.AddText(string.Format("{0}的行动被押后了", target.CustomName));
                //    }
                //    else
                //    {
                //        Fail();
                //    }
                //    break;
                //case MoveEffect.念动力:
                //    if (target.BeTelekinesis())
                //    {
                //        log.AddText(string.Format("{0}浮到了空中", target.CustomName));
                //    }
                //    else
                //    {
                //        Fail();
                //    }
                //    break;
                //case MoveEffect.防御平分:
                //    int total = 0;
                //    total = user.DefenceValue + target.DefenceValue;
                //    user.DefenceValue = total / 2;
                //    target.DefenceValue = total / 2;
                //    total = user.SpDefenceValue + target.SpDefenceValue;
                //    user.SpDefenceValue = total / 2;
                //    target.SpDefenceValue = total / 2;
                //    log.AddText("双方的防御平分了");

                //    break;
                //case MoveEffect.力量平分:
                //    int total = 0;
                //    total = user.AttackValue + target.AttackValue;
                //    user.AttackValue = total / 2;
                //    target.AttackValue = total / 2;
                //    total = user.SpAttackValue + target.SpAttackValue;
                //    user.SpAttackValue = total / 2;
                //    target.SpAttackValue = total / 2;
                //    log.AddText("双方的攻击平分了");

                //    break;
            }
        }
    }
}
